# -*- coding: utf-8 -*-
from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError


class ServerActions(models.Model):
    """ Add wecom option in server actions. """
    _name = 'ir.actions.server'
    _description = 'Server Action'
    _inherit = ['ir.actions.server']

    state = fields.Selection(selection_add=[
        ('fkc_portal', 'FKC Portal'),
        ], ondelete={'fkc_portal': 'cascade'})
    # Template
    fkc_portal_message_template_id = fields.Many2one('fkc.portal.message.template', string='Template', ondelete="restrict",
                                        domain="[('model_id', '=', model_id)]")

    def _run_action_fkc_portal_multi(self, eval_context=None):
        self.ensure_one()
        assert self.fkc_portal_message_template_id, "FKC Portal Message Templates not set?"
        # Clean context from default_type to avoid making attachment
        # with wrong values in subsequent operations
        cleaned_ctx = dict(self.env.context)
        # 如果有需要的话，这里可以清除掉一些不需要的context
        # cleaned_ctx.pop('default_type', None)
        # cleaned_ctx.pop('default_parent_id', None)
        self.fkc_portal_message_template_id.with_context(cleaned_ctx).send_message(
            self._context.get('active_ids'), force_send=True)
        return False

    @api.model
    def _get_eval_context(self, action=None):
        """ Override the method giving the evaluation context but also the
        context used in all subsequent calls. Add the mail_notify_force_send
        key set to False in the context. This way all notification emails linked
        to the currently executed action will be set in the queue instead of
        sent directly. This will avoid possible break in transactions. """
        eval_context = super(ServerActions, self)._get_eval_context(action=action)
        ctx = dict(eval_context['env'].context)
        # ctx['mail_notify_force_send'] = False
        # eval_context['env'].context = ctx
        return eval_context
